
package com.modele.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.main.EasyLunch;
import com.modele.dao.DAO;
import com.modele.entity.Consomptible;
import com.modele.entity.Menu;


/**
 * @author Haniki
 *
 */
public class MenuDAO  extends DAO<Menu>{

	/**
	 * @param connexion
	 */
	public MenuDAO(Connection connexion) {
		super(connexion);
	}

	@Override
	public boolean create(Menu obj) {
		try {
			PreparedStatement statement = connexion.prepareStatement("INSERT INTO menu " +
					"(nomMenu, prix) " +
					"VALUES (?,?)");
			statement.setString(1, obj.getNomMenu());
			statement.setFloat(2, obj.getPrix());

			return statement.executeUpdate() == 1;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;

	}

	@Override
	public boolean delete(Menu obj) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(Menu obj) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Menu find(int id) {
		
		Menu menu = null;
		
		try 
		{
			PreparedStatement statement = connexion.prepareStatement("SELECT * FROM menu WHERE idMenu = ?");
			statement.setInt(1, id);
			
			if(statement.execute())
			{
				ResultSet result = statement.getResultSet();
				if(result.next())
				{
					menu = new Menu();

					menu.setIdMenu(id);
					menu.setNomMenu(result.getString(2));
					menu.setPrix(result.getFloat(3));
					
					List<Consomptible> listConsomptible = findCompositionMenu(id);
					
					menu.setListConsomptibles(listConsomptible);
				}
			}
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return menu;
	}
	
	public List<Consomptible> findCompositionMenu(int idMenu)
	{
		List<Consomptible> listConsomptible = new ArrayList<Consomptible>();
		
		try 
		{
			PreparedStatement statement = connexion.prepareStatement("SELECT * FROM contenuMenu WHERE idMenu = ?");
			statement.setInt(1, idMenu);
			
			
			if(statement.execute())
			{
				ResultSet result = statement.getResultSet();
			
				Consomptible consomptible;
				
				while(result.next())
				{
					consomptible = EasyLunch.getFactory().getConsomptibleDAO().find(result.getInt(2));
					
					listConsomptible.add(consomptible);
				}
			}
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return listConsomptible;
	}
	
	@Override
	public List<Menu> findAll() 
	{
		List<Menu> listMenu = new ArrayList<Menu>();
		
		try 
		{
			PreparedStatement statement = connexion.prepareStatement("SELECT * FROM menu");
			
			if(statement.execute())
			{
				ResultSet result = statement.getResultSet();
				
				Menu menu;
				
				while(result.next())
				{
					menu = new Menu();

					int id = result.getInt(1);
					
					menu.setIdMenu(id);
					menu.setNomMenu(result.getString(2));
					menu.setPrix(result.getFloat(3));
					
					List<Consomptible> listConsomptible = findCompositionMenu(id);
					
					menu.setListConsomptibles(listConsomptible);
					
					listMenu.add(menu);
				}
			}
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return listMenu;
	}

	@Override
	public Menu findByValue(Object obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isPresent(Object obj) {
		// TODO Auto-generated method stub
		return false;
	}
}


